/*
 * This file is part of Smart Route for S60 3rd Edition terminals
 * 
 * Copyright (C) 2006  Harri Salminen

 * This program free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 *(at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 

#ifndef CSmartRouteDataStore_H
#define CSmartRouteDataStore_H

// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include <eikenv.h>
#include <D32DBMS.H>

_LIT(KDatabaseFile, "\\private\\20006A29\\routes.dat");
_LIT(KDatabaseFileEmulator, "c:\\private\\20006A29\\routes.dat");

_LIT(KPointTable, "point");
_LIT(KPointIndex, "pointIndex");
_LIT(KAltitudeCol, "altitude");
_LIT(KLatitudeCol, "latitude");
_LIT(KLongitudeCol, "longitude");
_LIT(KRouteCol, "route");

_LIT(KRouteTable, "route");
_LIT(KRouteIndex, "routeIndex");
_LIT(KRouteNameCol, "name");
_LIT(KRouteStartCol, "start");
_LIT(KRouteEndCol, "end");
_LIT(KRouteMaxSpeedCol, "maxspeed");
_LIT(KRouteDistanceCol, "distance");
_LIT(KRouteDurationCol, "duration");
_LIT(KIdCol, "id");

// CLASS DECLARATION
class CSmartRouteDataStore;

/**
 * One point at the route
 */
class TRoutePoint
	{
	public:
		TReal64 iLatitude;
		TReal64 iLongitude;
		TReal32 iAltitude; 
	};

/**
 *  Holds just the route's short name for screen display and route id.
 */
#define KShortNameLen 32

class TShortRoute
	{
	public:
		TBuf<KShortNameLen> iShortName;
		TTime iStart;
		TTime iEnd;
		TUint32 iId;
	};
	
/**
 * Different file types that can be exported
 */
enum TFileExportType
	{
	EKml ///< Google Earth's KML-file
	};

typedef RArray<TRoutePoint> RRoutePointArray;
typedef RArray<TShortRoute> RShortRouteArray; 

	
/**
 * A complete route with a set of points
 */
class CSmartRoute : public CBase
	{
	friend class CSmartRouteDataStore;
	public:
		~CSmartRoute();
		static CSmartRoute* NewL(); ///< Creates a new route
		
		TUint32 Id() const;
		
		void SetNameL(const TDesC& aName); ///< Name for the route
		const TDesC& Name(); //If name is not set, returns the start time as name
		
		void SetStart(const TTime& aStart);
		void SetEnd(const TTime& aEnd);
		const TTime& Start() const; 
		const TTime& End() const; 
		
		void SetMaxSpeed(TReal32 aMaxSpeed); ///Maximum speed m/s
		const TReal32& MaxSpeed() const;
		
		void SetDistance(TReal32 aDistance); 
		const TReal32& Distance() const;
		
		void SetDuration(TInt32 aDuration);
		const TInt32& Duration() const;
		
		void AddPointL(const TRoutePoint& aPoint);
		const RRoutePointArray& Points() const;
		void ExportL(const TDesC& aFileName, const TFileExportType aType); 
		
	protected:
		CSmartRoute();
		void ConstructL();
		
		void SetId(const TUint32& aId);
		
	private:
		RRoutePointArray iRoutePoints;
		HBufC* iName;
		TTime iStart; 
		TReal32 iMaxSpeed; 
		TReal32 iDistance;
		TTime iEnd;
		TUint32 iId; 
		TInt32 iDuration; ///<Duration in seconds
	};

/**
*  CSmartRouteDataStore
* 
*/
class CSmartRouteDataStore : public CBase
{
public: 
	~CSmartRouteDataStore();
	static CSmartRouteDataStore* NewL();
	
	void SaveRouteL(CSmartRoute& aRoute); ///< Stores route to database.
	CSmartRoute* RestoreRouteL(const TUint32 aRouteId); ///< Reads route information from database
	void DeleteRouteL(const TUint32 aRouteId); ///< Deletes a route and saved points
	void ListRoutesL(RShortRouteArray& aRouteList);  ///< Fills a list of saved routes
	
private:
	CSmartRouteDataStore();
	void ConstructL();
	
	void CreateTablesL();
	
private: //Data	
	RDbNamedDatabase iDatabase;
};

#endif // CSmartRouteDataStore_H
